package com.jeff.tianti.controller;

import com.jeff.tianti.cms.dto.AdvSpaceQueryDTO;
import com.jeff.tianti.cms.dto.CertificateQueryDTO;
import com.jeff.tianti.cms.dto.ColumnInfoQueryDTO;
import com.jeff.tianti.cms.entity.AdvSpace;
import com.jeff.tianti.cms.entity.Certificate;
import com.jeff.tianti.cms.entity.ColumnInfo;
import com.jeff.tianti.cms.entity.Course;
import com.jeff.tianti.cms.service.AdvSpaceService;
import com.jeff.tianti.cms.service.CertificateService;
import com.jeff.tianti.cms.service.ColumnInfoService;
import com.jeff.tianti.cms.service.FriendShipLinkService;
import com.jeff.tianti.common.dto.AjaxResult;
import com.jeff.tianti.common.entity.PageModel;
import com.jeff.tianti.org.dto.DictionaryDTO;
import com.jeff.tianti.org.entity.Dictionary;
import com.jeff.tianti.org.service.DictionaryService;
import com.jeff.tianti.util.Constants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/certificate")
public class CertificateController {
    @Autowired
    ColumnInfoService columnInfoService;
    @Autowired
    CertificateService certificateService;

    /**
     * 获取 友联列表
     *
     * @return
     */
    @RequestMapping("/list")
    public String index(HttpServletRequest request, Model model) {
        String currentPageStr = request.getParameter("currentPage");
        String pageSizeStr = request.getParameter("pageSize");
        String number = request.getParameter("number");
        String columnId = request.getParameter("columnId");

        ColumnInfoQueryDTO columnInfoQueryDTO = new ColumnInfoQueryDTO();
        columnInfoQueryDTO.setCode(Constants.CODE_ZSLX);
        columnInfoQueryDTO.setIsCodeLike(false);
        List<ColumnInfo> columnInfos = columnInfoService.queryColumnInfoList(columnInfoQueryDTO);
        List<ColumnInfo> certificateCateList = null;

        if (!CollectionUtils.isEmpty(columnInfos)) {
            ColumnInfoQueryDTO parentColumnInfoQueryDTO = new ColumnInfoQueryDTO();
            parentColumnInfoQueryDTO.setIsRootColumnLike(false);
            parentColumnInfoQueryDTO.setRootColumnId(columnInfos.get(0).getId());
            certificateCateList = columnInfoService.queryColumnInfoList(parentColumnInfoQueryDTO);
        }

        int currentPage = 1;
        int pageSize = 10;
        if (StringUtils.isNotBlank(currentPageStr)) {
            currentPage = Integer.parseInt(currentPageStr);
        }
        if (StringUtils.isNotBlank(pageSizeStr)) {
            pageSize = Integer.parseInt(pageSizeStr);
        }

        CertificateQueryDTO certificateQueryDTO = new CertificateQueryDTO();
        certificateQueryDTO.setNumber(number);
        certificateQueryDTO.setColumnId(columnId);
        certificateQueryDTO.setCurrentPage(currentPage);
        certificateQueryDTO.setPageSize(pageSize);

        PageModel<Certificate> page = certificateService.queryCertificatePage(certificateQueryDTO);
        List<Certificate> list = page.getList();
        for (int i = 0; i < list.size(); i++) {
            Certificate certificate = list.get(i);
            String columnIdStr = certificate.getColumnId();
            ColumnInfo columnInfo =null;
            if (StringUtils.isNotBlank(columnIdStr)){
                columnInfo= columnInfoService.find(columnIdStr);
            }

            if (columnInfo != null) {
                certificate.setCertificateCate(columnInfo.getName());
            }
        }
        model.addAttribute("page", page);
        model.addAttribute("certificateQueryDTO", certificateQueryDTO);
        model.addAttribute("certificateCateList", certificateCateList);
        model.addAttribute(Constants.MENU_NAME, Constants.MENU_CERTIFICATE);
        return "/cms/certificate/certificate_list";
    }

    /**
     * 跳转到友情编辑页
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/edit")
    public String edit(HttpServletRequest request, Model model) {

        String id = request.getParameter("id");
        Certificate certificate = certificateService.find(id);

        ColumnInfoQueryDTO columnInfoQueryDTO = new ColumnInfoQueryDTO();
        columnInfoQueryDTO.setCode(Constants.CODE_ZSLX);
        columnInfoQueryDTO.setIsCodeLike(false);
        List<ColumnInfo> columnInfos = columnInfoService.queryColumnInfoList(columnInfoQueryDTO);
        List<ColumnInfo> certificateCateList = null;

        if (!CollectionUtils.isEmpty(columnInfos)) {
            ColumnInfoQueryDTO parentColumnInfoQueryDTO = new ColumnInfoQueryDTO();
            parentColumnInfoQueryDTO.setIsRootColumnLike(false);
            parentColumnInfoQueryDTO.setRootColumnId(columnInfos.get(0).getId());
            certificateCateList = columnInfoService.queryColumnInfoList(parentColumnInfoQueryDTO);
        }


        model.addAttribute("certificateCateList", certificateCateList);
        model.addAttribute("certificate", certificate);

        return "/cms/certificate/dialog/certificate_edit";
    }

    /**
     * 栏目保存操作
     *
     * @param request
     * @return
     */
    @RequestMapping("/ajax/save")
    @ResponseBody
    public AjaxResult doFriendShipAjaxSave(HttpServletRequest request) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);
        try {
            String id = request.getParameter("id");
            String img = request.getParameter("cerImg");
            String columnId = request.getParameter("columnId");
            String number = request.getParameter("number");
            String name = request.getParameter("name");
            Certificate certificate = new Certificate();
            certificate.setId(id);
            certificate.setImg(img);
            certificate.setNumber(number);
            certificate.setName(name);
            certificate.setColumnId(columnId);

            if (StringUtils.isNotBlank(id)) {
                certificateService.updateSelective(certificate);
            } else {
                certificateService.save(certificate);
            }
            ajaxResult.setSuccess(true);
        } catch (Exception e) {
            ajaxResult.setMsg(e.getMessage());
            e.printStackTrace();
        }

        return ajaxResult;
    }

    /**
     * 栏目保存操作
     *
     * @param request
     * @return
     */
    @RequestMapping("/ajax/del")
    @ResponseBody
    public AjaxResult doFriendShipAjaxDel(HttpServletRequest request) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);
        try {
            String id = request.getParameter("id");
            certificateService.delete(id);
            ajaxResult.setSuccess(true);
        } catch (Exception e) {
            ajaxResult.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return ajaxResult;
    }

}
